package com.evenmo.hrms.mapper.oldmapper.dynaSqlProvider;

import com.evenmo.hrms.pojo.User;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

// 动态sql
public class UserDynaSqlProvider {
    public String updateUserByParams(User user) {

        return new SQL() {
            {
                UPDATE("user");
                if (user.getUserName() != null) {
                    SET("userName=#{userName}");
                }
                if (user.getUserStatus() != null) {
                    SET("userStatus=#{userStatus}");
                }
                if (user.getCreateTime() != null) {
                    SET("createTime=#{createTime}");
                }
                if (user.getPassword() != null) {
                    SET("password=#{password}");
                }
                if (user.getLoginName() != null) {
                    SET("loginName=#{loginName}");
                }
                WHERE("id=#{id}");
            }
        }.toString();
    }


    public String insertUser(User user) {
        return new SQL() {
            {
                INSERT_INTO("user");
                if (user.getUserName() != null) {
                    VALUES("userName", "#{userName}");
                }
                if (user.getUserStatus() != null) {
                    VALUES("userStatus", "#{userStatus}");
                }
                if (user.getCreateTime() != null) {
                    VALUES("createTime", "#{createTime}");
                }
                if (user.getLoginName() != null) {
                    VALUES("loginName", "#{loginName}");
                }
                if (user.getPassword() != null) {
                    VALUES("password", "#{password}");
                }


            }
        }.toString();
    }

    public String selectUserByParam(User user) {
        return new SQL() {
            {
                SELECT("*");
                FROM("user");
                if (user.getId() != null) {
                    WHERE("id=#{id}");
                }
                if (user.getLoginName() != null) {
                    WHERE("loginName=#{loginName}");
                }
                if (user.getPassword() != null) {
                    WHERE("password=#{password}");
                }
                if (user.getUserStatus() != null) {
                    WHERE("userStatus=#{userStatus}");
                }
                if (user.getCreateTime() != null) {
                    WHERE("createTime=#{createTime}");
                }
                if (user.getUserName() != null) {
                    WHERE("userName=#{userName}");
                }
            }
        }.toString();
    }

    public String countByParam(Map<String, Object> param) {
        return new SQL() {

            {

                SELECT("count(1)");
                FROM("user");
                Object obj = param.get("user");
                if (obj != null) {
                    User user = (User) obj;

                    if (user.getUserName() != null && user.getUserName() != "") {
                        WHERE("userName=#{user.userName}");
                        OR();
                        WHERE("userName like concat('%',#{user.userName},'%')");

                        // 危险写法，有sql注入风险，不建议查询参数用拼接写法
//                        WHERE("userName like '%"+user.getUserName()+"%'");
                    }
                    if (user.getUserStatus() != null) {
                        WHERE("userStatus=#{user.userStatus}");
                    }

                }


            }
        }.toString();
    }
}
